<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\BehaviorCategory;
use app\common\model\BehaviorRecord;
use app\common\model\CommentRule;
use app\common\model\Grade;
use think\Validate;

/**
 * 学校
 *
 * @package app\api\controller
 */
class School extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiTitle    (学校列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/lists)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579067524",
    "data": [
    {
    "id": 36,
    "name": "房山长阳第一小学",
    "province_id": 1,
    "city_id": 2,
    "district_id": 10,
    "address": "长阳镇起步区3号",
    "logo": "http://mp.uxuj.test/uploads/20191118/b4037c02a840db9dd1a78fa6ca932e1e.jpg",
    "note": null,
    "school_id": 3,
    "admin_id": 0,
    "created_at": 1573873298
    }
    ]
    })
     */
    public function lists()
    {
        $list = \app\common\model\School::where('id', $this->auth->school_id)->select();

        $this->success('', $list);
    }

    /**
     * @ApiTitle    (学校基本信息)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/profile)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="学校ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579067665",
    "data": {
    "id": 36,
    "name": "房山长阳第一小学",
    "province_id": 1,
    "city_id": 2,
    "district_id": 10,
    "address": "长阳镇起步区3号",
    "logo": "http://mp.uxuj.test/uploads/20191118/b4037c02a840db9dd1a78fa6ca932e1e.jpg",
    "note": null,
    "school_id": 3,
    "admin_id": 0,
    "created_at": 1573873298
    }
    })
     */
    public function profile()
    {
        $id = $this->request->request('id', 0);
        $result = \app\common\model\School::where('id', $id)->find();
        $this->success('', $result);
    }

    /**
     * @ApiTitle    (学校详细信息)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/detail)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="学校ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579067685",
    "data": {
    "id": 36,
    "name": "房山长阳第一小学",
    "province_id": 1,
    "city_id": 2,
    "district_id": 10,
    "address": "长阳镇起步区3号",
    "logo": "http://mp.uxuj.test/uploads/20191118/b4037c02a840db9dd1a78fa6ca932e1e.jpg",
    "note": null,
    "school_id": 3,
    "admin_id": 0,
    "created_at": 1573873298,
    "student_count": 13,
    "classes_count": 5,
    "teacher_count": 10
    }
    })
     */
    public function detail()
    {
        $id = $this->request->request('id', 0);
        $result = \app\common\model\School::withCount('student,classes,teacher')
            ->where('id', $id)
            ->find();
        $this->success('', $result);
    }

    /**
     * @ApiTitle    (年级列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/gardes)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="学校ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579068642",
    "data": [
    {
    "id": 1,
    "name": "一年级",
    "stage": 1,
    "key": 1
    },
    {
    "id": 2,
    "name": "二年级",
    "stage": 1,
    "key": 2
    },
    {
    "id": 3,
    "name": "三年级",
    "stage": 1,
    "key": 3
    },
    {
    "id": 4,
    "name": "四年级",
    "stage": 1,
    "key": 4
    },
    {
    "id": 5,
    "name": "五年级",
    "stage": 1,
    "key": 5
    },
    {
    "id": 6,
    "name": "六年级",
    "stage": 1,
    "key": 6
    },
    {
    "id": 7,
    "name": "初一",
    "stage": 2,
    "key": 7
    },
    {
    "id": 8,
    "name": "初二",
    "stage": 2,
    "key": 8
    },
    {
    "id": 9,
    "name": "初三",
    "stage": 2,
    "key": 9
    },
    {
    "id": 10,
    "name": "高一",
    "stage": 3,
    "key": 10
    },
    {
    "id": 11,
    "name": "高二",
    "stage": 3,
    "key": 11
    },
    {
    "id": 12,
    "name": "高三",
    "stage": 3,
    "key": 12
    },
    {
    "id": 13,
    "name": "学前班",
    "stage": 0,
    "key": 0
    },
    {
    "id": 14,
    "name": "其他",
    "stage": 14,
    "key": 13
    }
    ]
    })
     */
    public function grades()
    {
        $list = Grade::all();
        $this->success('', $list);
    }

    /**
     * @ApiTitle    (班级列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/school/classes)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="学校ID", sample="")
     * @ApiParams   (name="grade_id", type="int", required=false, description="年级ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1585715635",
    "data": [
    {
    "id": 1,
    "name": "一年级",
    "stage": 1,
    "key": 1,
    "classes": [
    {
    "id": 52,
    "name": "1班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 30,
    "headmaster": 42,
    "slogan": null,
    "logo": null,
    "department_id": 3,
    "created_at": 1573873470,
    "type": 1,
    "record_count": 1
    },
    {
    "id": 97,
    "name": "实验室",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 35,
    "headmaster": 48,
    "slogan": "书上得来终觉浅,绝知此事要躬行",
    "logo": null,
    "department_id": 3,
    "created_at": 1574666014,
    "type": 1,
    "record_count": 0
    },
    {
    "id": 98,
    "name": "3班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 30,
    "headmaster": 42,
    "slogan": "11111111111",
    "logo": "https://mp.uxuj.cn/uploads/20191126/3147709ada9058d8ee3878759bce9046.png",
    "department_id": 3,
    "created_at": 1574752789,
    "type": 1,
    "record_count": 0
    },
    {
    "id": 101,
    "name": "初二一班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 37,
    "headmaster": 48,
    "slogan": " 好好学习天天向上",
    "logo": "",
    "department_id": 3,
    "created_at": 1574838417,
    "type": 1,
    "record_count": 0
    },
    {
    "id": 102,
    "name": "展会一班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 38,
    "headmaster": 42,
    "slogan": " ",
    "logo": "",
    "department_id": 3,
    "created_at": 1574999870,
    "type": 2,
    "record_count": 0
    },
    {
    "id": 103,
    "name": "展会二班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 39,
    "headmaster": 44,
    "slogan": " ",
    "logo": "",
    "department_id": 3,
    "created_at": 1575014500,
    "type": 2,
    "record_count": 0
    },
    {
    "id": 104,
    "name": "展会三班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 40,
    "headmaster": 45,
    "slogan": " ",
    "logo": "",
    "department_id": 3,
    "created_at": 1575014541,
    "type": 2,
    "record_count": 0
    },
    {
    "id": 105,
    "name": "展会四班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 41,
    "headmaster": 46,
    "slogan": " ",
    "logo": "",
    "department_id": 3,
    "created_at": 1575014577,
    "type": 2,
    "record_count": 0
    },
    {
    "id": 106,
    "name": "展会五班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 42,
    "headmaster": 47,
    "slogan": " ",
    "logo": "",
    "department_id": 3,
    "created_at": 1575014609,
    "type": 2,
    "record_count": 0
    },
    {
    "id": 107,
    "name": "展会六班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 43,
    "headmaster": 48,
    "slogan": " ",
    "logo": "",
    "department_id": 3,
    "created_at": 1575014645,
    "type": 2,
    "record_count": 0
    },
    {
    "id": 108,
    "name": "展会七班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 44,
    "headmaster": 63,
    "slogan": " ",
    "logo": "",
    "department_id": 3,
    "created_at": 1575014679,
    "type": 2,
    "record_count": 0
    },
    {
    "id": 109,
    "name": "展会八班",
    "school_id": 36,
    "grade_id": 1,
    "classroom_id": 45,
    "headmaster": 64,
    "slogan": " ",
    "logo": "",
    "department_id": 3,
    "created_at": 1575014712,
    "type": 2,
    "record_count": 0
    }
    ]
    },
    {
    "id": 2,
    "name": "二年级",
    "stage": 1,
    "key": 2,
    "classes": [
    {
    "id": 94,
    "name": "五班",
    "school_id": 36,
    "grade_id": 2,
    "classroom_id": 33,
    "headmaster": 42,
    "slogan": " ",
    "logo": null,
    "department_id": 3,
    "created_at": 1574060399,
    "type": 1,
    "record_count": 0
    },
    {
    "id": 96,
    "name": "odin",
    "school_id": 36,
    "grade_id": 2,
    "classroom_id": 34,
    "headmaster": 51,
    "slogan": " ",
    "logo": null,
    "department_id": 3,
    "created_at": 1574665709,
    "type": 1,
    "record_count": 0
    }
    ]
    },
    {
    "id": 3,
    "name": "三年级",
    "stage": 1,
    "key": 3,
    "classes": []
    },
    {
    "id": 4,
    "name": "四年级",
    "stage": 1,
    "key": 4,
    "classes": []
    },
    {
    "id": 5,
    "name": "五年级",
    "stage": 1,
    "key": 5,
    "classes": []
    },
    {
    "id": 6,
    "name": "六年级",
    "stage": 1,
    "key": 6,
    "classes": []
    },
    {
    "id": 7,
    "name": "初一",
    "stage": 2,
    "key": 7,
    "classes": [
    {
    "id": 99,
    "name": "初一一班",
    "school_id": 36,
    "grade_id": 7,
    "classroom_id": 36,
    "headmaster": 48,
    "slogan": " 所向披靡",
    "logo": "",
    "department_id": 3,
    "created_at": 1574820304,
    "type": 1,
    "record_count": 0
    },
    {
    "id": 100,
    "name": "初一二班",
    "school_id": 36,
    "grade_id": 7,
    "classroom_id": 36,
    "headmaster": 48,
    "slogan": " 积极向上",
    "logo": "",
    "department_id": 3,
    "created_at": 1574835026,
    "type": 1,
    "record_count": 0
    }
    ]
    },
    {
    "id": 8,
    "name": "初二",
    "stage": 2,
    "key": 8,
    "classes": []
    },
    {
    "id": 9,
    "name": "初三",
    "stage": 2,
    "key": 9,
    "classes": []
    },
    {
    "id": 10,
    "name": "高一",
    "stage": 3,
    "key": 10,
    "classes": []
    },
    {
    "id": 11,
    "name": "高二",
    "stage": 3,
    "key": 11,
    "classes": []
    },
    {
    "id": 12,
    "name": "高三",
    "stage": 3,
    "key": 12,
    "classes": []
    },
    {
    "id": 13,
    "name": "学前班",
    "stage": 0,
    "key": 0,
    "classes": []
    },
    {
    "id": 14,
    "name": "其他",
    "stage": 14,
    "key": 13,
    "classes": []
    }
    ]
    })
     */
    public function classes()
    {
        $id = $this->request->request('id', 0);
        $grade_id = $this->request->request('grade_id', 0);
        $keywords = $this->request->request('keywords', '');

        if (!$id) {
            $this->error('参数有误');
        }

        $list = Grade::with(['classes' => function ($query) use ($id, $keywords) {
            $query->where('school_id', $id);
            if ($keywords) {
                $query->whereLike('name', "%$keywords%");
            }
        }])
            ->where(function ($query) use ($grade_id) {
                if ($grade_id) {
                    $query->where('id', $grade_id);
                }
            })->select();

        if ($list) {
            $list = collection($list)->toArray();

            //当前时间段是否可以点评
            //$rule = CommentRule::checkRule(BehaviorCategory::CLASSES);

            $class_ids = [];
            array_map(function ($value) use (&$class_ids) {
                $class_ids = array_merge($class_ids, array_column($value['classes'], 'id'));
            }, $list);

//            $behaviorRecords = BehaviorRecord::where('behavior_category_type', BehaviorCategory::CLASSES)
//                ->where('record_user_id', $this->auth->id)
//                ->whereIn('class_id', $class_ids)
//                ->whereTime('created_at', 'today')
//                ->column('class_id');

            $behaviorRecords = BehaviorRecord::where('behavior_category_type', BehaviorCategory::CLASSES)
                ->where('record_user_id', $this->auth->id)
                ->whereIn('class_id', $class_ids)
                ->whereTime('created_at', 'today')
                ->group('class_id')
                ->column('class_id,count(*) count');

            foreach ($list as $key => $value) {
                foreach ($value['classes'] as $k => $class) {
                    if (!strstr($class['logo'], 'http') && strstr($class['logo'], '/uploads/')) {
                        $list[$key]['classes'][$k]['logo'] = config('mp.url') . $class['logo'];
                    }

                    if (isset($behaviorRecords[$class['id']])) {
                        $list[$key]['classes'][$k]['record_count'] = $behaviorRecords[$class['id']];
                    } else {
                        $list[$key]['classes'][$k]['record_count'] = 0;
                    }

//                    $ret = in_array($class['id'], $behaviorRecords);
//                    $list[$key]['classes'][$k]['can_comment'] = $rule && !$ret ? true : false;
//                    $list[$key]['classes'][$k]['can_edit'] = $rule && $ret ? true : (!$rule && $ret ? true : false);
                }
            }

        }

        $this->success('', $list);

    }

}
